*************************************************************************
*Table A.10: Heterogeneous effects by household characteristics
*This table reports differential estimated treatment effects along dimensions of household characteristics for ration card-level outcomes in endline one (January- March)

*************************************************************************

use "${SurveyDataDir}/JH_ePOS_HH_DataforAnalysis.dta",clear

*******************************************************************************
* Keep a dataset including surveyed households and ghosts 
*******************************************************************************
keep if ss_code == "SS01" | ghost_final == 1

preserve
tempfile HHdata_withghosts
save `HHdata_withghosts'
restore

*******************************************************************************
*keep a dataset that drops ghosts only use this in receipts analysis
*******************************************************************************
preserve

keep if ss_code == "SS01"

drop if ghost_final==1
tempfile HHdata_surveyedEL1
save `HHdata_surveyedEL1'
restore

*******************************************************************************
svyset [pw = pweight]


count if ghost_final == 1
scalar ghosts = r(N)
scalar obs = 3960   

*Get relative weights of AAY and PH rationcard holders by whether RC is in an urban area
sum pweight if rationcardtype == "AAY" & isurban == 0
scalar AAY_weight0 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 0
scalar PH_weight0 = r(sum)

sum pweight if rationcardtype == "AAY" & isurban == 1
scalar AAY_weight1 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 1
scalar PH_weight1 = r(sum)


*************************************************************************
program mydisp, eclass
         matrix betass=e(b)
         matrix colnames betass = "one" "two" "three" "four"
         mat list betass
         ereturn repost b = betass, rename
 end
*************************************************************************


*Binary Dimensions that we want to measure heterogeneity of 
loc hetVar "nSTSC a7_education_max_ind g7_ann_total_income_ind_y0"

*Outcomes that we have monthly measurement of & want to report unpooled
loc Moutcomes "c_total_access_cost_adj_mar17"
loc BLoutcomesM "c_cost_to_access_y0"

*Pooled outcome with BL value
*Analysis sample includes ghost HH
loc ELoutcomesBLg "value_total"
loc BLoutcomesg "value_total_y0"



*Outcome that we only have monthly measure monthly measurement & no BL lag
loc ELoutcomes "c6_WTA"


*counting number of obs that belong to each category:

*1) scheduled caste (1 if does not belong - f2_caste_hh_STSC==0)

tab f2_caste_hh_STSC

*Yes
count if f2_caste_hh_STSC==0
loc N_caste_yes=r(N)
di in red "`N_caste_yes'"

*No
count if f2_caste_hh_STSC==1
loc N_caste_no=r(N)
di in red "`N_caste_no'"


* 2) education 
tab  a7_education_max_ind

*Yes
count if a7_education_max_ind==1
loc N_edu_yes=r(N)
di in red "`N_edu_yes'"

*No
count if a7_education_max_ind==0
loc N_edu_no=r(N)
di in red "`N_edu_no'"




*3) Income 
tab g7_ann_total_income_ind_y0

*Yes
count if g7_ann_total_income_ind_y0==1
loc N_inc_yes=r(N)
di in red "`N_inc_yes'"

count if g7_ann_total_income_ind_y0==0
loc N_inc_no=r(N)
di in red "`N_inc_no'"

*4) isurban

tab isurban 

*Yes
count if isurban==1
loc N_urban_yes=r(N)
di in red "`N_urban_yes'"

*No
count if isurban==0
loc N_urban_no=r(N)
di in red "`N_urban_no'"


* 5) max_signal_strength_med

tab max_signal_strength_med


*6) signal_strength_med_1

tab signal_strength_med_1

*Yes
count if signal_strength_med_1==1
loc N_signal_yes=r(N)
di in red "`N_signal_yes'"

*No
count if signal_strength_med_1==0
loc N_signal_no=r(N)
di in red "`N_signal_no'"

**********************************************
* Analysis
**********************************************
loc h = 0


foreach Hvar of local hetVar{
loc h = `h'+ 1
loc i = 0
loc o = 0

*** Access cost ***
use `HHdata_surveyedEL1',clear
gen nSTSC = (f2_caste_hh_STSC == 0)
replace nSTSC = . if mi(f2_caste_hh_STSC)

gen TX`Hvar' = treatment*`Hvar'

foreach Ovar of local Moutcomes{
loc o = `o'+1

loc i = `i' + 1
local lag: word `i' of `BLoutcomesM'
unab blvarname: `lag'
gen bl_var = `blvarname'
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1


eststo temp: xi: reg `Ovar' treatment TX`Hvar' i.strata bl_var bl_var_mi [pw = pweight], cluster(block_code)
eststo het`h'`o'0: lincomest treatment
estimates restore temp
eststo het`h'`o'1: lincomest treatment + TX`Hvar'
estimates restore temp
eststo het`h'`o'd: lincomest TX`Hvar' 

drop bl_var*
}


*** Total value ***
*Outcomes for 3 months pooled with BL (samples that include ghost HH)
*Analysis sample that uses ghost HH
use `HHdata_withghosts',clear
gen nSTSC = (f2_caste_hh_STSC == 0)
replace nSTSC = . if mi(f2_caste_hh_STSC)

gen TX`Hvar' = treatment*`Hvar'

loc i = 0
foreach Ovar of local ELoutcomesBLg{
loc i = `i' + 1
local lag: word `i' of `BLoutcomesg'
unab blvarname: `lag'
gen bl_var = `blvarname'
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1


loc o = `o'+1

preserve
keep `Ovar'_mar17 `Ovar'_feb17 `Ovar'_jan17 treatment  TX`Hvar' strata pweight block_code uid bl_var bl_var_mi
rename `Ovar'_jan17 `Ovar'1
rename `Ovar'_feb17 `Ovar'2
rename `Ovar'_mar17 `Ovar'3

reshape long `Ovar', i(uid) j(month)


eststo temp: xi: reg `Ovar' treatment TX`Hvar' i.strata bl_var bl_var_mi [pw = pweight], cluster(block_code)
eststo het`h'`o'0: lincomest treatment
estimates restore temp
eststo het`h'`o'1: lincomest treatment + TX`Hvar'
estimates restore temp
eststo het`h'`o'd: lincomest TX`Hvar'

restore

drop bl_var*
}

*** WTA ***
foreach Ovar of local ELoutcomes{
loc o = `o'+1
preserve
gen val_rec_mar17 = value_total_mar17 > 0
gen val_rec_feb17 = value_total_feb17 > 0
gen val_rec_jan17 = value_total_jan17 > 0
keep `Ovar'_mar17 `Ovar'_feb17 `Ovar'_jan17 treatment  TX`Hvar' strata pweight block_code uid
rename `Ovar'_jan17 `Ovar'1
rename `Ovar'_feb17 `Ovar'2
rename `Ovar'_mar17 `Ovar'3
reshape long `Ovar', i(uid) j(month)


eststo temp: xi: reg `Ovar' treatment TX`Hvar' i.strata [pw = pweight], cluster(block_code)
eststo het`h'`o'0: lincomest treatment	

estimates restore temp
eststo het`h'`o'1: lincomest treatment + TX`Hvar'

estimates restore temp
eststo het`h'`o'd: lincomest TX`Hvar'

di in red "`het`h'`o'd"

restore

}

*** Value received binary ***
loc o = `o'+1
preserve
***Calculate binary for value received for each month
gen val_rec_mar17 = value_total_mar17 > 0
gen val_rec_feb17 = value_total_feb17 > 0
gen val_rec_jan17 = value_total_jan17 > 0
keep val_rec_mar17 val_rec_feb17 val_rec_jan17 treatment  TX`Hvar' strata pweight block_code uid
rename val_rec_jan17 val_rec1
rename val_rec_feb17 val_rec2
rename val_rec_mar17 val_rec3
reshape long val_rec, i(uid) j(month)


eststo temp: xi: reg val_rec treatment TX`Hvar' i.strata [pw = pweight], cluster(block_code)
eststo het`h'`o'0: lincomest treatment	

estimates restore temp
eststo het`h'`o'1: lincomest treatment + TX`Hvar'

estimates restore temp
eststo het`h'`o'd: lincomest TX`Hvar'

di in red "`het`h'`o'd"

restore

eststo het`h'0: appendmodels  het`h'20 het`h'30 het`h'10 het`h'40
eststo het`h'0: mydisp
eststo het`h'1: appendmodels  het`h'21 het`h'31 het`h'11 het`h'41
eststo het`h'1: mydisp
eststo het`h'd: appendmodels  het`h'2d het`h'3d het`h'1d het`h'4d
eststo het`h'd: mydisp

}




*Italicize obs counts for each category
loc str_caste_yes = "\emph{\textit{(N=`N_caste_yes')}}"
loc str_caste_no = "\emph{\textit{(N=`N_caste_no')}}"

loc str_edu_yes = "\emph{\textit{(N=`N_edu_yes')}}"
loc str_edu_no = "\emph{\textit{(N=`N_edu_no')}}"

loc str_inc_yes = "\emph{\textit{(N=`N_inc_yes')}}"
loc str_inc_no = "\emph{\textit{(N=`N_inc_no')}}"


loc str_urban_yes = "\emph{\textit{(N=`N_urban_yes')}}"
loc str_urban_no = "\emph{\textit{(N=`N_urban_no')}}"

loc str_signal_yes = "\emph{\textit{(N=`N_signal_yes')}}"
loc str_signal_no = "\emph{\textit{(N=`N_signal_no')}}"



*Output table:
#delimit ;
							
esttab het10 het11 het1d het20 het21 het2d het30 het31 het3d using "${OutputDir}/TableA_10.tex",
	label b(%12.2g) se(%12.2g) booktabs replace nocons width(\hsize)
	varlabels(one "Value received (market prices)" two "Value received (WTA)" three "Transaction costs" four "Whether received value")
	mgroups("HH is upper caste?" "\specialCellCenter{HH above median \\education level?}" "\specialCellCenter{HH above median \\annual income?}",	
			pattern(1 0 0 1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) 
			span erepeat(\cmidrule(lr){@span}))
	mlabel(
	"\specialCellCenter{No \\ `str_caste_no'} " "\specialCellCenter{Yes \\ `str_caste_yes'} " "$\Delta$" 
	
	"\specialCellCenter{No \\ `str_edu_no'} " "\specialCellCenter{Yes \\ `str_edu_yes'} " "$\Delta$" 
	
	"\specialCellCenter{No \\ `str_inc_no'} " "\specialCellCenter{Yes \\ `str_inc_yes'} " "$\Delta$"
	)
	star(* .10 ** .05 *** .01) 
	substitute("\multicolumn{10}{l}{\footnotesize \textit{t} statistics in parentheses}\\" " "
							"\multicolumn{10}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\" ""
							"\hline\hline" "\hline"
							"Standard errors in parentheses" " "
							"\sym{*} \(p<.10\), \sym{**} \(p<.05\), \sym{***} \(p<.01\)" " "
							"\midrule" " "
							"Observations" " "
							"\multicolumn{10}{l}{\footnotesize  }\\" " ");		
																						
#delimit cr 
eststo clear
program drop mydisp
